package jp.sourceforge.akjrcp.ffmpeg;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import jp.sourceforge.akjrcp.audio.MP3AudioSettingComposite;
import jp.sourceforge.akjrcp.audio.MpegAudioFormatUtil;
import jp.sourceforge.akjrcp.util.PathnameUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.ui.ISharedImages;


public class MP3FFMpegPreference extends AdvancedAudioFFMpegPreference{
public static Log log = LogFactory.getLog(MP3FFMpegPreference.class);


public String[] toCommand(FFMpegInput[] inputs,String output){
	FFMpegFLVParser parser=new FFMpegFLVParser(this.getFfmpegPath());
	String path=inputs[inputs.length-1].getPath();//last one
	for (int i = inputs.length-1; i >=0;i--) {
	
		FLVInfomation info=parser.parseFile(new File(inputs[i].getPath()));
		if(info.isHaveAudio() ){
			path=inputs[i].getPath();//this is audio
			break;
		}
	}
		return customToCommand(path, null,output);
	}


public String[] customToCommand(String input,String avsInput,String output){
	String inputType=PathnameUtils.getExtension(input);
	
	
	FFMpegFLVParser parser=new FFMpegFLVParser(this.getFfmpegPath());
	
	
	
	this.setEnableVideo(false);
	
	FLVInfomation info=parser.parseFile(new File(input));
	//error check if there are audio
	if(!info.isHaveAudio() ){
		if(avsInput==null){
		log.warn("no aduio stream");
		return null;
		}else{
			info=parser.parseFile(new File(avsInput));
			if(!info.isHaveAudio() ){
				log.warn("no aduio stream even in avs");
				return null;
				}
		}
	}
	
	String inputCodec=info.getAudioCodec();
	int inputChannel=info.getAudioChannel();
	if(inputChannel==0){
		log.info("input channel must not 0");
		//return null;
		}
	int inputFramerate=info.getAudioRate();
	if(inputFramerate==0){
		log.info("input inputFramerate must not 0");
		//return null;
		}
	
	int bitrate=info.getAudioBitrate();
	if(bitrate==0){
		log.warn("input bitrate must not 0");
		//some old ffmpeg happend this one.
		bitrate=getDefaultBitrate();
		//return null;
		}
	
	if(inputChannel==0 || inputFramerate==0){
		if(avsInput!=null){
		info=parser.parseFile(new File(avsInput));
		inputChannel=info.getAudioChannel();
		if(inputChannel==0){
			log.warn("inputAvs channel must not 0");
			return null;
			}
		inputFramerate=info.getAudioRate();
		if(inputFramerate==0){
			log.warn("inputAvs inputFramerate must not 0");
			return null;
			}
		}else{
			//no avs
			log.warn(inputChannel+","+inputFramerate+" there are no avs and not supported codec");
			return null;
		}
		
	}

	/*
	 * now can control eather enable avisynth or disable it  
	if(inputCodec.equals(FFMpegInfomation.MP3)){
		avsInput=null;//don't use avsInput anythime when audio codec is mp3
	}
	*/
	
	if(isForceConvert()){
		this.setAudioCodec(FFMpegOptions.CODEC_AUDIO_MP3);
		int tmpFramerate;
		if(getConvertFramerate()==MP3AudioSettingComposite.SAME_SOURCE){
			tmpFramerate=inputFramerate;
		}else{
			tmpFramerate=getConvertFramerate();
		}
		this.setAudioRate(tmpFramerate);
		
		//bitrate
		int tmpBitrate;
		int supportedBitrate[]=MpegAudioFormatUtil.getAvailableMP3Bitrate(inputFramerate);
		if(getConvertBitrate()==MP3AudioSettingComposite.SAME_SOURCE){
			tmpBitrate=MpegAudioFormatUtil.getSimilarValue(supportedBitrate, bitrate);
		}else{
			tmpBitrate=MpegAudioFormatUtil.getSimilarValue(supportedBitrate, getConvertBitrate());
		}
		this.setAudioBitrate(tmpBitrate);
		
		int tmpChannel;
		if(getConvertChannel()==MP3AudioSettingComposite.SAME_SOURCE){
			tmpChannel=inputChannel;
		}else{
			tmpChannel=getConvertChannel();
		}
		this.setAudioChannel(tmpChannel);
		
		
		setGuessSize(toKBytes(info.getDuration(), tmpBitrate));
	}else{
	if(isUseCopy() && (inputCodec.equals(FFMpegFileInfomation.MP3) || inputCodec.equals(FFMpegFileInfomation.MP3_0x0055)))	{
		this.setAudioCodec(FFMpegOptions.CODEC_COPY);
		avsInput=null;//don't use avsInput anythime when audio codec is mp3
		int audioB=info.getAudioBitrate();
		if(audioB==0){
			audioB=128;
		}
		setGuessSize(toKBytes(info.getDuration(), audioB));
		}
	else{
		this.setAudioCodec(FFMpegOptions.CODEC_AUDIO_MP3);
		//natural copy
		this.setAudioRate(inputFramerate);
		
		int tmpBitrate;
		int supportedBitrate[]=MpegAudioFormatUtil.getAvailableMP3Bitrate(inputFramerate);
			if(isCanUseBitrate(inputType)){
				//some codec bitrate differ form mp3's one
				tmpBitrate=MpegAudioFormatUtil.getSimilarValue(supportedBitrate, bitrate);
			}else{
				//maybe wav or non compressed audio
				tmpBitrate=MpegAudioFormatUtil.getSimilarValue(supportedBitrate, getDefaultBitrate());//try default value
			}
		this.setAudioBitrate(tmpBitrate);
		
		this.setAudioChannel(inputChannel);
		
		setGuessSize(toKBytes(info.getDuration(), tmpBitrate));
		}
	}
	
	if(input==null || output==null){
		//should exception
		return null;
	}
FFMpegInput inputs[]=new FFMpegInput[1];
	if(avsInput==null){
		
			inputs[0]=new FFMpegInput(input);
		}else{
			inputs[0]=new FFMpegInput(avsInput);
		}
	//call super input
	return super.toCommand(inputs,output);
	//return super.toCommand(input,avsInput, output);
	
	
}

}
